local skel = fk.CreateSkill{
  name = "tea__qiuqiu",
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__qiuqiu"] = "赇求",
  [":tea__qiuqiu"] = "出牌阶段限一次，你可以声明场上的一张牌，本回合你对场上有此牌的角色使用牌无距离和次数限制，且当你对其使用牌结算结束后，其可以移动之。结束阶段，若你为其，则你摸两张牌，否则你失去1点体力。",
  ["@@tea__qiuqiu-turn"] = "赇求",
  ["@tea__qiuqiu_mark-turn"] = "赇求",
  ["#tea__qiuqiu-choose"] = "赇求：选择一张牌",
  ["#tea__qiuqiu-invoke"] = "赇求：你可以移动指定装备",
  ["#tea__qiuqiu-select"] = "赇求：选择一名角色",
}
---@type ActiveSkillSpec
local qiuqiu = {
  mute = true,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and #to_select:getCardIds("ej") > 0
  end,
  can_use = function (self, player)
    return player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    room:notifySkillInvoked(player, skel.name, "control")
    player:broadcastSkillInvoke(skel.name, table.random{ 1, 2 })
    local to = effect.tos[1]
    local cid = room:askToChooseCard(player, {
      target = to,
      skill_name = skel.name,
      flag = "ej",
      prompt = "#tea__qiuqiu-choose"
    })
    if cid then
      room:doIndicate(player, { to })
      room:setPlayerMark(player, "@tea__qiuqiu_mark-turn", Fk:getCardById(cid).name)
      room:setCardMark(Fk:getCardById(cid), "@@tea__qiuqiu-turn", 1)
    end
  end
}
---@type TargetModSpec
local qiuqiu_targetmod = {
  bypass_distances = function (self, player, skill, card, to)
    if not to then return end
    return table.find(to:getCardIds("ej"), function (id)
      return Fk:getCardById(id):getMark("@@tea__qiuqiu-turn") > 0
    end)
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    if not to then return end
    return table.find(to:getCardIds("ej"), function (id)
      return Fk:getCardById(id):getMark("@@tea__qiuqiu-turn") > 0
    end)
  end
}
---@type TrigSkelSpec<UseCardFunc>
local qiuqiu_trig = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player == target then
      local room = player.room
      local cid
      local to = table.find(room:getAlivePlayers(), function (p)
      ---@diagnostic disable-next-line: return-type-mismatch
        return table.find(p:getCardIds("ej"), function (id)
          if Fk:getCardById(id):getMark("@@tea__qiuqiu-turn") > 0 and table.contains(data.tos, p) then
            cid = id
            return true
          end
        end)
      end)
      if to and cid then
        local targets = table.filter(room:getOtherPlayers(to), function(p)
          return to:canMoveCardInBoardTo(p, cid)
        end)
        event:setCostData(self, { from = to, card = cid, targets = targets })
        return #targets > 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(event:getCostData(self).from, {
      skill_name = skel.name,
      prompt = "#tea__qiuqiu-invoke"
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "special")
    local to = event:getCostData(self).from ---@type ServerPlayer
    local card = event:getCostData(self).card ---@type integer
    local targets = event:getCostData(self).targets ---@type ServerPlayer[]
    local toPlayer = room:askToChoosePlayers(to, {
      targets = targets,
      skill_name = skel.name,
      min_num = 1,
      max_num = 1,
      prompt = "#tea__qiuqiu-select"
    })
    if #toPlayer == 1 then
      room:moveCardTo(card, room:getCardArea(card), toPlayer[1], fk.ReasonJustMove, skel.name, nil, true, to)
    end
  end
}
---@type TrigSkelSpec<PhaseFunc>
local qiuqiu_delay = {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) and player == target and player.phase == Player.Finish and player:getMark("@tea__qiuqiu_mark-turn") ~= 0 then
      return table.find(player.room:getAlivePlayers(), function (p)
        ---@diagnostic disable-next-line: return-type-mismatch
        return table.find(p:getCardIds("ej"), function (id)
          return Fk:getCardById(id):getMark("@@tea__qiuqiu-turn") > 0
        end)
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if table.find(player:getCardIds("ej"), function (id)
      return Fk:getCardById(id):getMark("@@tea__qiuqiu-turn") > 0
    end) then
      player:drawCards(2, skel.name)
    else
      player:broadcastSkillInvoke(skel.name, 3)
      room:loseHp(player, 1, skel.name)
    end
  end
}
skel:addEffect("active", qiuqiu)
skel:addEffect("targetmod", qiuqiu_targetmod)
skel:addEffect(fk.CardUseFinished, qiuqiu_trig)
skel:addEffect(fk.EventPhaseStart, qiuqiu_delay)
return skel